﻿using Guolian.GForce.Extensions.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guoling.ProjectName.Data
{
    public class ApiDbContext : DbContextBase<ApiDbContext>
    {
        public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options)
        {

        }

        public DbSet<User> Users { get; set; }

        public DbSet<Order> Orders { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
            var databaseType = config.GetConnectionString("IsMySql");
            if (databaseType == null) throw new ArgumentNullException(nameof(databaseType));
            var databaseVersion = config.GetConnectionString("DataBaseType");
            if (databaseVersion == null) throw new ArgumentNullException(nameof(databaseVersion));
            if (bool.Parse(databaseType))
                optionsBuilder.UseMySql(config.GetConnectionString("DefaultDataBase"), new MySqlServerVersion(new Version(databaseVersion)));
        }
    }
}
